global infile "DataMain.dta"
global outfile "$results\Table 6"


* Create quartiles

use "$infile", clear
drop if dupindicator == 1

drop if m2 == .
gen m2water = water/m2

egen M2waterb = mean(m2water) if treat == 0, by(home)
egen m2waterb = mean(M2waterb), by(home)
egen M2watera = mean(m2water) if treat == 1, by(home)
egen m2watera = mean(M2watera), by(home)
gen m2waterm = (m2waterb + m2watera)/2

duplicates drop home, force

_pctile m2waterm, p(25)
local p25 = r(r1)
_pctile m2waterm, p(50)
local p50 = r(r1)
_pctile m2waterm, p(75)
local p75 = r(r1)

gen quartile = .
replace quartile = 1 if m2waterm <= `p25'
replace quartile = 2 if m2waterm > `p25' & m2waterm <= `p50' 
replace quartile = 3 if m2waterm > `p50' & m2waterm <= `p75' 
replace quartile = 4 if m2waterm > `p75' 

keep home quartile

save "Temp", replace


* Merging

use "$infile", clear
merge m:1 home using "Temp", nogen
save "Temp", replace


* Extra before-after estimates

use "Temp", clear 
keep if treated == 1 & quartile == 1
xtreg logwater treat i.moy, fe i(home) cluster(home)

use "Temp", clear 
keep if treated == 1 & quartile == 2
xtreg logwater treat i.moy, fe i(home) cluster(home)

use "Temp", clear 
keep if treated == 1 & quartile == 3
xtreg logwater treat i.moy, fe i(home) cluster(home)

use "Temp", clear 
keep if treated == 1 & quartile == 4
xtreg logwater treat i.moy, fe i(home) cluster(home)


* Table 6. Diff-in-diff estimates of quartile effects

use "Temp", clear 
drop if treated == 1 & quartile != 1
gen dataquartile = 1
save "TempA", replace
reghdfe logwater treat, absorb(datasethome datasetweek) cluster(home)
leveleffects
outreg2 using "$outfile", keep(treat) nocon 2aster dec(3) word ap ///
addstat(pre, $prem, effect, $effectm, transbeta, $transbeta) 

use "Temp", clear 
drop if treated == 1 & quartile != 2
gen dataquartile = 2
save "TempB", replace
reghdfe logwater treat, absorb(datasethome datasetweek) cluster(home)
leveleffects
outreg2 using "$outfile", keep(treat) nocon 2aster dec(3) word ap ///
addstat(pre, $prem, effect, $effectm, transbeta, $transbeta) 

use "Temp", clear 
drop if treated == 1 & quartile != 3
gen dataquartile = 3
save "TempC", replace
reghdfe logwater treat, absorb(datasethome datasetweek) cluster(home)
leveleffects
outreg2 using "$outfile", keep(treat) nocon 2aster dec(3) word ap ///
addstat(pre, $prem, effect, $effectm, transbeta, $transbeta) 

use "Temp", clear 
drop if treated == 1 & quartile != 4
gen dataquartile = 4
save "TempD", replace
reghdfe logwater treat, absorb(datasethome datasetweek) cluster(home)
leveleffects
outreg2 using "$outfile", keep(treat) nocon 2aster dec(3) word ap ///
addstat(pre, $prem, effect, $effectm, transbeta, $transbeta) 


* F-tests

use "TempA", clear
append using "TempB"
append using "TempC"
append using "TempD"

forvalues i = 1(1)4 {
	gen treat`i' = 0
	replace treat`i' = treat if dataquartile == `i'
}
foreach var in datasethome datasetweek home {
	forvalues i = 1(1)4 {
		gen `var'`i' = 0
		replace `var'`i' = `var' if dataquartile == `i'
	}
	egen s`var' = group(`var'1 `var'2 `var'3 `var'4)
}

reghdfe logwater treat1 treat2 treat3 treat4, absorb(sdatasethome sdatasetweek) cluster(home)
test _b[treat1] = _b[treat2] = _b[treat3] = _b[treat4] 

